/*
 * @(#)SearchContenx.java, 2024/2/22 11:08
 *
 * Copyright (c) 2018-2024, 四川蜀天梦图数据科技有限公司.
 * All rights reserved.
 */
package controllers;

import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * SearchContenx
 *
 * @author fantazy
 * @version 1.0, 2024年02月22日
 */
public class SearchContext
{

    private List<SearchListener> listeners;

    public SearchContext(SearchListener searchListener)
    {
        this.listeners = new ArrayList<>();
        listeners.add(searchListener);
    }

    public ScoreDoc[] doLuceneSearch(final IndexSearcher is, Query q) throws IOException
    {
        try
        {
            listeners.forEach(listener -> {
                listener.beforeSearch();
            });
            return is.search(q, Integer.MAX_VALUE).scoreDocs;
        }
        finally
        {
            listeners.forEach(listener -> {
                listener.afterSearch();
            });
        }
    }

    public static interface SearchListener
    {
        void beforeSearch();

        void afterSearch();
    }
}
